<script lang="ts" setup>
import {ref,defineExpose,nextTick} from 'vue'
import categorySelcet from './categorySelcet.vue'
import {ProductAddSerive,ProductUpdateSerive} from '@/api/product'
import { ElMessage } from 'element-plus'
const dialogFormVisible = ref(false)

const defaultModel = {
  product_id:'',
   in_product_id:null,
   product_name:'',
   category:'',
   unit:'',
   inventory_number:0,
   unit_price:0,
   in_note:''
}
const from = ref()
const form = ref(defaultModel)
const formLabelWidth:string = '120px' 

const emit = defineEmits(['success'])
const submit = async()=>{
  await from.value.validate()
  if(form.value.product_id){
     await ProductUpdateSerive(form.value)
      console.log('执行了更新操作')
     ElMessage.success('更新成功！')
      dialogFormVisible.value = false
        emit('success','update')
  }else{
    await ProductAddSerive(form.value)
    ElMessage.success('添加成功！')
    emit('success','add')
    dialogFormVisible.value = false
  }
}
    
const open = (row:any)=>{
    dialogFormVisible.value = true

      // 等待DOM更新后重置表单验证  
     nextTick(() => {  
          from.value.resetFields()  // 清除上一次的验证状态  
           form.value = {...row}   
        })  
    
}

const rules = {
   in_product_id:[
    { required:true,message:'入库编号不得为空',trigger:'blur'},
  ],
   product_name:[
    { required:true,message:'产品名称不得为空',trigger:'blur'},
   ],
   category:[
    { required:true,message:'产品类别不得为空',trigger:'blur'},
   ],
   unit:[
    { required:true,message:'单位不得为空',trigger:'blur'},
   ],
   inventory_number:[
    { required:true,message:'产品单价不得为空',trigger:'blur'},
   ],
   unit_price:[
    { required:true,message:'入库编号不得为空',trigger:'blur'},
   ],

}
defineExpose({
    open
})
</script>

<template>
<div class="wrapped">


<el-dialog v-model="dialogFormVisible" :title="form.product_id? '编辑':'添加'" width="500">
    <el-form :model="form" :rules="rules" ref="from">
      <el-form-item label="入库编号：" :label-width="formLabelWidth" prop="in_product_id">
        <el-input v-model="form.in_product_id" placeholder="请输入入库编号" />
      </el-form-item>
      <el-form-item label="产品名称：" :label-width="formLabelWidth" prop="product_name">
       <el-input  v-model="form.product_name"  placeholder="请输入产品名称" />
      </el-form-item>
           <el-form-item  label="产品类别：" :label-width="formLabelWidth" prop="category">
            <categorySelcet v-model="form.category" />
        </el-form-item>
          <el-form-item label="单位：" :label-width="formLabelWidth" prop="unit">
       <el-input v-model="form.unit"  placeholder="请输入单位"/>
      </el-form-item>

    <el-form-item label="入库数量：" :label-width="formLabelWidth" prop="inventory_number">
       <el-input v-model="form.inventory_number" placeholder="请输入入库数量"   />
      </el-form-item>

    <el-form-item label="产品单价：" :label-width="formLabelWidth" prop="unit_price">
       <el-input v-model="form.unit_price" placeholder="请输入产品单价"   />
      </el-form-item>
          <el-form-item label="入库备注：" :label-width="formLabelWidth" >
       <el-input v-model="form.in_note"   type="textarea"  :autosize="{ minRows: 4, maxRows: 4 }" placeholder="请输入入库备注"   />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">
          取消
        </el-button>
      </div>
    </template>
  </el-dialog>
      </div>
</template>

<style lang="scss" scoped>
</style>